package com.supervision.manage.service;

import com.supervision.domain.bean.dc.SdkApp;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SmsService
 * @description:
 * @author: whis
 * @create: 2020-05-03 20:36
 **/
@Service
public class SmsService {

    private static Logger logger = LoggerFactory.getLogger(UserAuthenticator.class);

    @Autowired
    private TencentSdkService tencentSdkService;

    public SendSmsResponse send(String phone, String templateId, Map<String, String> data) {
        SdkApp.SmsApp smsApp = SdkApp.initSmsAppConfig();

        SmsClient smsClient = tencentSdkService.initSmsClient();
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppid(smsApp.getAppId());
        req.setSign(smsApp.getSmsSign());
        req.setTemplateID(templateId);
        req.setPhoneNumberSet(new String[]{"+86" + phone});

        List<String> list = new LinkedList<>();
        for (Map.Entry<String, String> entry : data.entrySet()) {
            list.add(entry.getValue());
        }
        String[] params = new String[list.size()];
        list.toArray(params);
        req.setTemplateParamSet(params);

        SendSmsResponse res = null;
        try {
            res = smsClient.SendSms(req);
            System.out.println(SendSmsResponse.toJsonString(res));
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }

        return res;
    }

//    public HTTPResponse send(String phone, String templateId, Map<String, String> data) {
//        SdkApp.SmsApp smsApp = SdkApp.initSmsAppConfig();
//        SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(smsApp.getAppId()), smsApp.getAppKey());
//        try {
//            List<String> list = new LinkedList<>();
//            for (Map.Entry<String, String> entry : data.entrySet()) {
//                list.add(entry.getValue());
//            }
//            String[] params = new String[list.size()];
//            list.toArray(params);
//            logger.info("手机号：{}，模板ID：{}，签名：{}", phone, templateId, smsApp.getSmsSign());
//            SmsSingleSenderResult result = ssender.sendWithParam("86", phone, Integer.parseInt(templateId), params, smsApp.getSmsSign(), "", "");
//            HTTPResponse response = result.getResponse();
//            if (response.statusCode != 200) {
//                logger.error("[{}]发送短信失败，模板ID -> {}, 原因 -> {}", response.statusCode, templateId, response.reason);
//            }
//            return response;
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (JSONException e) {
//            e.printStackTrace();
//        } catch (HTTPException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
}
